<?php


namespace app\common\model;

use app\common\model\Common as CommonModel;

class Contact extends CommonModel
{
    protected $pk = 'id';

    /**
     * index页面数据获取
     * */
    public function tableData($post,$field='*',$isPage = true)
    {
        if (isset($post['limit'])) {
            $limit = $post['limit'];
        } else {
            $limit = 10;
        }

        $tableWhere = $this->tableWhere($post);
        $query      = $this
            ->field($field)
            ->where($tableWhere['where'])->whereOr($tableWhere['whereOr'])->order($tableWhere['order']);


        if ($isPage) {
            $list        = $query->paginate([
                'list_rows'=> $limit,//每页数量
                'query' => request()->param(),
            ]);
            $data        = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $page = $list->render();
            $re['count'] = $list->total();
        } else {
            $list        = $query->select();
            $data        = $this->tableFormat($list);         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $re['count'] = count($list);
            $page = [];
        }

        $re['code'] = 1;
        $re['msg']  = '';
        $re['data'] = $data;
        $re['page'] = $page;
        return $re;
    }


    protected function tableWhere($post)
    {
        $where = $whereOr = [];
        if(isset($post['name']) && !empty($post['name'])){
            $where[] = ['name','like','%'.$post['name'].'%'];
        }
        if(isset($post['mobile']) && !empty($post['mobile'])){
            $where[] = ['mobile','like','%'.$post['mobile'].'%'];
        }

        $result['where']   = $where;
        $result['whereOr'] = $whereOr;

        $result['field'] = "*";
        $pk = $this->pk;
        $result['order'] = [$pk => 'desc'];
        return $result;
    }

    protected function tableFormat($list)
    {
        $Users = new Users();
        foreach($list as $k=>$v){
            $u_one = $Users->where('id','=',$v['user_id'])->find();
            $list[$k]['user_name'] = $u_one['name'];
            $list[$k]['add_time'] = date('Y-m-d',$v['add_time']);
        }
        return $list;
    }


    public function handle($param){
        $result = [
            'code' => 0,
            'msg'    => '',
            'data'   => [],
        ];
        $act = $param['act'];
        
        $data['user_id'] = isset($param['user_id'])?$param['user_id']:'';
        $data['name'] = isset($param['name'])?$param['name']:'';
        $data['mobile'] = isset($param['mobile'])?$param['mobile']:'';
        $data['content'] = isset($param['content'])?$param['content']:'';

        if($act == 'add'){
            $data['add_time'] = time();
            $r = $this->save($data);
        }
        if($act == 'edit'){
            $data['update_time'] = time();
            $r = $this->where('id','=',$param['id'])->update($data);
        }
        if(!$r){
            $result['msg'] = '提交失败';
            return $result;
        }
        $result['code'] = 1;
        $result['msg'] = '提交成功';
        return $result;
    }


}